<template>
  <div center>
    <el-form ref="info2" :inline="true" :rules="rules" :model="info" size="mini" label-position="right"
             label-width="110px">
      <el-row>
        <el-col :span="8">
          <el-form-item label="受监测单位" prop="sjcdwmc">
            <el-input v-model="info.sjcdwmc" placeholder="请输入内容" :disabled="true"/>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="检测编号" prop="jcbh">
            <el-input v-model="info.jcbh" placeholder="请输入内容" :disabled="true"/>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="调查日期" prop="dcrq">
            <el-date-picker
              v-model="info.dcrq"
              :disabled="isShow"
              type="date"
              value-format="yyyy-MM-dd"
              placeholder="选择日期">
            </el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-form-item label="工作场所及检测时工作内容描述" prop="gzcsgznr">
          <el-input type="textarea" autosize v-model="info.gzcsgznr" placeholder="请输入内容" :disabled="isShow" :rows="4"/>
        </el-form-item>
      </el-row>

      <el-divider></el-divider>

      <el-form-item v-if="!isShow">
        <el-button type="warning" size="mini" @click="addTable1(null,null)">添加</el-button>
      </el-form-item>
      <el-table key="table1" border :data="info.table1Json" style="width: 100%">
        <el-table-column
          prop="jobWord"
          align="center"
          label="作业工种/岗位">
        </el-table-column>
        <el-table-column
          prop="jobNum"
          align="center"
          label="岗位总人数">
        </el-table-column>
        <el-table-column
          prop="shiftNumber"
          align="center"
          label="最大班次数">
        </el-table-column>
        <el-table-column
          prop="number"
          align="center"
          label="写实人数">
        </el-table-column>
        <el-table-column
          prop="name"
          align="center"
          label="姓名">
        </el-table-column>
        <el-table-column
          prop="jobAge"
          align="center"
          label="工龄">
        </el-table-column>
        <el-table-column
          prop="workTime"
          align="center"
          label="工作时间">
        </el-table-column>
        <el-table-column
          prop="workContent"
          align="center"
          label="工作内容">
        </el-table-column>
        <el-table-column
          prop="harm"
          align="center"
          label="接触职业病危害因素">
        </el-table-column>
        <el-table-column
          prop="note"
          align="center"
          label="备注">
        </el-table-column>
        <el-table-column
          align="center"
          fixed="right"
          label="操作" v-if="!isShow">
          <template slot-scope="scope">
            <el-button @click="addTable1(scope.$index,scope.row)" type="text" size="small">编辑</el-button>
            <el-button type="text" size="small" @click="delTable1(scope.$index,scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-divider></el-divider>
      <el-form-item label="调查人" prop="dcr">
        <el-select v-model="info.dcr" :disabled="isShow" multiple placeholder="请选择" :multiple-limit="2">
          <el-option
            v-for="item in userList"
            :label="item.nickName"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="受检单位陪同人" prop="sjdwptr">
        <el-input type="text" autosize v-model="info.sjdwptr" placeholder="打印后手签" disabled/>
      </el-form-item>
    </el-form>
    <el-divider></el-divider>
    <div style="text-align: center" v-if="!isShow">
      <el-button type="primary" size="small" @click="addOrEdit">保存</el-button>
    </div>
    <el-dialog :visible.sync="centerDialogVisible" append-to-body center width="80%">
      <el-form ref="tableInfo" :inline="true" :rules="rules" :model="tableInfo" size="mini" label-position="right"
               label-width="200px">
        <el-row>
          <el-col :span="8">
            <el-form-item label="作业工种/岗位" prop="jobWord">
              <el-input v-model="tableInfo.jobWord" placeholder="请输入内容"/>
            </el-form-item>
            <el-form-item label="岗位总人数" prop="jobNum">
              <el-input v-model="tableInfo.jobNum" placeholder="请输入内容"/>
            </el-form-item>
            <el-form-item label="最大班次数" prop="shiftNumber">
              <el-input v-model="tableInfo.shiftNumber" placeholder="请输入内容"/>
            </el-form-item>
            <el-form-item label="写实人数" prop="number">
              <el-input v-model="tableInfo.number" placeholder="请输入内容"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="姓名" prop="name">
              <el-input v-model="tableInfo.name" placeholder="请输入内容"/>
            </el-form-item>
            <el-form-item label="工龄" prop="jobAge">
              <el-input v-model="tableInfo.jobAge" placeholder="请输入内容"/>
            </el-form-item>
            <el-form-item label="工作时间" prop="workTime">
              <el-input v-model="tableInfo.workTime" placeholder="请输入内容"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="工作内容" prop="workContent">
              <el-input v-model="tableInfo.workContent" placeholder="请输入内容"/>
            </el-form-item>
            <el-form-item label="接触职业病危害因素" prop="harm">
              <el-input v-model="tableInfo.harm" placeholder="请输入内容"/>
            </el-form-item>
            <el-form-item label="备注" prop="note">
              <el-input v-model="tableInfo.note" placeholder="请输入内容"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="mini" @click="centerDialogVisible = false">关 闭</el-button>
        <el-button type="primary" size="mini" @click="addOrEditTable1">确 定
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>


<script>
import imgUpdate from '@/components/Upload/imgUpdate'
import {getInfo, addOrEdit} from '@/api/table/tableThree'
import {getUserList} from '@/api/system/sysUser'
import {getUserId} from '@/utils/auth'

export default {
  components: {
    imgUpdate
  },
  props: {
    noCreated: {
      type: Boolean,
      default: null
    },
    isShow: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      info: {},
      userId: getUserId(),
      userList: [],
      tableInfo: {},
      tableIndex: null,
      centerDialogVisible: false,
      centerDialogVisible2: false,
      rules: {
        dzr: [{required: true, message: '请输入内容', trigger: 'chang'}],
        sjcdwptr: [{required: true, message: '请输入内容', trigger: 'chang'}],
        cyrq: [{required: true, message: '请选择日期', trigger: 'chang'}],
        dzrq: [{required: true, message: '请选择日期', trigger: 'chang'}],
        shr: [{required: true, message: '请选择内容', trigger: 'chang'}],
        dcr: [{required: true, message: '请选择内容', trigger: 'chang'}]
      }
    }
  }, created() {
    this.getUserList()
    this.info.table1Json = []
    // this.info.table2Json = []
  }, methods: {
    // 新增或编辑表1
    addTable1(index, row) {
      this.tableIndex = index
      this.tableInfo = {}
      if (row != null) {
        this.tableInfo = row
      }
      this.centerDialogVisible = true
    },
    // 保存表1
    addOrEditTable1() {
      if (this.tableIndex == null) {
        this.info.table1Json.push(this.tableInfo)
      } else {
        this.info.table1Json.splice(this.tableIndex, 1, this.tableInfo)
      }
      this.centerDialogVisible = false
    },
    // 删除表1
    delTable1(index, row) {
      this.info.table1Json.splice(index, 1)
    },
    // 获取所有用户
    getUserList() {
      getUserList().then(response => {
        this.userList = response.data
      })
    },
    getInfo(id) {
      getInfo(id).then(response => {
        if (response.data.table1Json != null) {
          response.data.table1Json = JSON.parse(response.data.table1Json)
        } else {
          response.data.table1Json = []
        }
        if (response.data.dcr != null && response.data.dcr != '') {
          response.data.dcr = JSON.parse(response.data.dcr)
          var ids = []
          for (let i = 0; i < response.data.dcr.length; i++) {
            ids.push(response.data.dcr[i].id)
          }
          response.data.dcr = ids
        } else {
          response.data.dcr = []
        }
        this.info = response.data
      })
    },
    addOrEdit: function () {
      this.$refs['info2'].validate((valid) => {
        if (valid) {
          addOrEdit(this.info.id, this.info.tableOneId, this.info.dcrq, this.info.gzcsgznr, JSON.stringify(this.info.dcr), this.info.sjdwptr, JSON.stringify(this.info.table1Json)).then(response => {
            if (this.info.id == null) {
              // 新增
              this.$notify({
                title: '成功',
                message: '创建成功',
                type: 'success',
                duration: 2000
              })
              this.$parent.$parent.$parent.$parent.$parent.getData()
            } else {
              // 编辑
              this.$notify({
                title: '成功',
                message: '更新成功',
                type: 'success',
                duration: 2000
              })
            }
            response.data.table1Json = JSON.parse(response.data.table1Json)
            // response.data.table2Json = JSON.parse(response.data.table2Json)
            if (response.data.dcr != null && response.data.dcr != '') {
              response.data.dcr = JSON.parse(response.data.dcr)
              var ids = []
              for (let i = 0; i < response.data.dcr.length; i++) {
                ids.push(response.data.dcr[i].id)
              }
              response.data.dcr = ids
            }
            this.info = response.data
            this.$parent.$parent.$parent.$parent.$parent.centerDialogVisible = false
          })
        }
      })
    },
    // 初始化
    init(id) {
      this.getInfo(id)
    }
  }
}
</script>

